<template>
	<view style="padding: 32rpx;">
		<view class="scroll_head">
			<view @click="showIndex=index" :class="showIndex===index?'isActive':''" class="tab_item" v-for="(item,index) in tabList" :key="index">
				<text>{{item}}</text>
			</view>
		</view>
		<!-- 这下面不用v-show，对小程序可能无法筛选显示 -->
		<scroll-view scroll-y="true" class="scroll_content" @scrolltolower="getMore" >
			<!-- 案例 -->
			<view class="common_case" v-if="showIndex===0">
				<view @click="toShowDetail(item.id)" class="case_item" v-for="(item,index) in list[showIndex]" :key="index">
					<img :src="item.logo" :alt="item.title" />
					<text>{{ item.title }}</text>
				</view>
			</view>
			
			<!-- 新品 -->
			<view v-else-if="showIndex===1" class="newcase_item" v-for="(item,index) in list[showIndex]">
				<text>{{ item.product }}</text>
				<img :src="item.logo" :alt="item.title" />
				<button @click="toShowDetail(item.id)">立即查看</button>
			</view>
			
			<!-- 资讯 -->
			<view class="recommendation_list"  v-else-if="showIndex===2">
				<view @click="toShowDetail(item.id)" class="recommendation_item" v-for="(item,index) in list[showIndex]">
					<img class="recommendation_item_img" :src="item.cover!=1?item.cover:'https://www.mopkeji.com/uploads/20210518/a4024106e11f447bd503b1bad8e2b0c0.jpg'"
					 :alt="item.title" />
					<view class="item_right">
						<text style="font-family: PingFang SC, PingFang SC;font-weight: bold;font-size: 28rpx;color: #000000;min-height: 28rpx;">{{ item.title?item.title:'' }}</text>
						<text style="margin-top: 8rpx;min-height: 22rpx;">{{getAbstract(item.content)}}</text>
						<view class="function">
							<text>{{ item.createtime?item.createtime.split(' ')[0]:getDate }}</text>
							<view class="recommendation_function">
								<view>
									<img src="/static/home/view.png" alt="浏览数" />
									{{ item.view?item.view:0 }}
								</view>
								<view>
									<img src="/static/home/good.png" alt="点赞数" />
									{{ item.likes?item.likes:0 }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabList:["案例","新品","资讯"],
				showIndex:0,
				form:[{
					pageNo:1,
					pageSize:10
				},{
					pageNo:1,
					pageSize:10
				},{
					pageNo:1,
					pageSize:10
				}],
				list:[],
				isFinish:[false,false,false],
				nowDate: new Date(),
				userId:2
			}
		},
		computed: {
			getDate() {
				const date = new Date(this.nowDate);
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const formattedDate = `${year}-${month}-${day}`;
				return formattedDate
			}
		},
		methods: {
			async getIndex(){
				for(let i = 0;i<this.tabList.length;i++){
					let res = await this.$api.collectQuery({
						type: i+1,
						userId: this.userId,
						...this.form[i]
					});
					if(!res?.success){
						this.$refs.uToast.show({type:'error',title: "数据获取失败!"});
						return;
					}
					const result = res.result;
					this.list[i] = i===2?result.carinformationList:result.carCaseList;
					this.isFinish[i] = result.total<=result.size;
				}
			},
			getAbstract(content){
				const reg = /<[^>]+>/gi; //筛选标签
				if(content)
					return content.split(reg).filter(
					item => (item !== '')&&(item !== '\n')).join('。');
				else return '';
			},
			async getMore(){
				let that = this;
				if(!that.isFinish[that.showIndex]){
					that.form[that.showIndex].pageNo++;
					let res = await that.$api.collectQuery({
						type: that.showIndex+1,
						userId: that.userId,
						...that.form[that.showIndex]
					});
					if(!res?.success){
						this.$refs.uToast.show({type:'error',title: "数据获取失败!"});
						return;
					}
					const result = res.result;
					that.list[that.showIndex].push(...result.records);
					that.isFinish[that.showIndex] = result.total<=that.list[that.showIndex].length;
				}
			},
			// 显示详情
			async toShowDetail(id){
				let url = '';
				switch(this.showIndex){
					case 0:
						url = '/pages/car/caseDetail?id=' + id;
						break;
					case 1:
						url = '/pages/car/caseDetail?id=' + id + '&isNew=' + true;
						break;
					case 2:
						url = '/pages/dynamic/detail?id=' + id;
						break;
				}
				if(url) uni.navigateTo({url: url});
			}
		},
		created() {
			// 判断是否登录，获取userid
			if(this.$store.state.phone_login_token){
				this.userId = JSON.parse(this.$store.state.user_message).id;
			}
		},
		async onLoad(){
			await this.getIndex();
			this.$forceUpdate();
		}
	}
</script>
<style>
	page{
		background-color: #F5F5F5;;
	}
</style>
<style scoped lang="scss">
.scroll_head{
	overflow-x: scroll;
	display: flex;
	align-items: center;
	margin-bottom: 32rpx;
	.tab_item{
		width: 120rpx;
		height: 56rpx;
		margin-right: 16rpx;
		border-radius: 896rpx 896rpx 896rpx 896rpx;
		background: #FFFFFF;
		padding: 8rpx 32rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-weight: 400;
		font-size: 28rpx;
		color: #969799;
	}
}
.isActive{
	background-color: #FBCB11 !important;
	color: #0D0D0D !important;
}
.scroll_content{
	display: flex; 
	flex-wrap: wrap;
	align-items: center;
	width: 100%;
	height: 90vh;
	.common_case{
		width: 100%;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		.case_item{
			display: flex;
			flex-direction: column;
			margin-bottom: 32rpx;
			width: 49%;
			img{
				width: 100%;
				height: 252rpx;
				background: #DBDBDB;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				border: 2rpx solid #EBEDF0;
			}
			text{
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #323233;
				line-height: 44rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
		}
	}
	.newcase_item{
		width: 686rpx;
		height: 672rpx;
		background-color: #FFFFFF;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		text{
			margin: 32rpx 0rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: rgba(0,0,0,0.8);
		}
		img{
			width: 590rpx;
			height: 444rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			margin-bottom: 28rpx;
		}
		button{
			background: #000000;
			border-radius: 548rpx 548rpx 548rpx 548rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
			width: 190rpx;
			height: 72rpx;
			&:active{
				background-color: #292929;
				color: #cecece;
			}
		}
	}
	.recommendation_item{
		display: flex;
		flex-wrap: wrap;
		padding: 2rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 22rpx;
		color: rgba(0,0,0,0.65);
		background: #FFFFFF;
		margin-bottom: 16rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		.recommendation_item_img{
			width: 208rpx;
			height: 156rpx;
			margin-right: 24rpx;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
		}
		.item_right{
			display: flex;
			flex: 1;
			flex-direction: column;
			padding: 20rpx 0rpx 12rpx 24rpx;
			margin-right: 24rpx;
			overflow: hidden;
			white-space: nowrap;
			text{
				overflow: hidden;
				word-break: break-all;
				text-overflow: ellipsis;
			}
			.function{
				margin-top: 26rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-family: DIN, DIN;
				color: rgba(0,0,0,0.45);
				.recommendation_function{
					display: flex;
					align-items: center;
					view{
						margin-right: 32rpx;
						display: flex;
						align-items: center;
						img{
							width: 22rpx;
							height: 22rpx;
							margin-right: 4rpx;
						}
					}
				}
			}
		}
	}
}
@media screen and (max-width: 680rpx){
		.recommendation_item .recommendation_item_img{
			width: 100%;
			aspect-ratio: 11/8;
			margin-right: 0rpx;
		}
	}
</style>
